<template>
  <div class="app-container">

    <!-- 搜索菜单栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="学号" prop="studentNo">
        <el-input
          v-model="queryParams.studentNo"
          placeholder="请输入学号"
          clearable
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级"
          clearable
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宿舍栋号" prop="buildingNo">
        <el-select
          v-model="queryParams.buildingNo"
          placeholder="宿舍栋号"
          clearable
          size="small"
          style="width: 180px"
        >
          <el-option
            v-for="dict in buildingNoOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍楼层" prop="storey">
        <el-select
          v-model="queryParams.storey"
          placeholder="宿舍楼层"
          clearable
          size="small"
          style="width: 180px"
        >
          <el-option
            v-for="dict in storeyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="下月到期" prop="nextMonthExpire">
        <el-date-picker
          v-model="queryParams.nextMonthExpire"
          type="date"
          style="width: 260px"
          value-format="yyyy-MM-dd"
          placeholder="选择任意日期，查询下月数据">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="缴费状态" prop="payStatus">
        <el-select
          v-model="queryParams.payStatus"
          placeholder="请选择"
          clearable
          size="small"
        >
          <el-option
            v-for="item in payStatusOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >同步寄宿学生数据</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          >excel导入缴费信息</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="sdRentList">
      <!-- <el-table-column type="selection" width="40" align="center" /> -->
      <el-table-column
        type="index"
        :index="table_index"
        label="序号"
        align="center"
        width="80"
      />          
      <el-table-column label="学号" align="center" prop="studentNo" :show-overflow-tooltip="true" />
      <el-table-column label="姓名" align="center" prop="studentName" :show-overflow-tooltip="true" />
      <el-table-column label="班级" align="center" prop="className" :show-overflow-tooltip="true" />
      <el-table-column label="宿舍栋号" align="center" prop="buildingNo" :show-overflow-tooltip="true"/>
      <el-table-column label="宿舍号" align="center" prop="dormitoryNo" :show-overflow-tooltip="true" />
      <el-table-column label="押金" align="center" prop="deposit" :show-overflow-tooltip="true" />
      <el-table-column label="入住日期" align="center" prop="checkInDate" :show-overflow-tooltip="true" />
      <el-table-column label="下次缴费日期" align="center" prop="nextPayDate" :show-overflow-tooltip="true" />
      <el-table-column label="缴费状态" align="center" prop="payStatus" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="140" >
        <template slot-scope="scope">

          <el-button
            size="mini"
            circle type="success"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          />
          <el-button
            size="mini"
            circle type="info"
            icon="el-icon-view"
            @click="handleViewPayRecorList(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 数据导入 -->
    <el-dialog
      :title="upload.uploadTitle"
      :visible.sync="upload.uploadOpen"
      width="400px"
    >
      <el-upload
        ref="upload"
        :limit="1"
        name="upload"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm" :disabled="submitDisabled">确 定</el-button>
        <el-button @click="upload.uploadOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 缴费 -->
    <el-dialog title="缴费" :visible.sync="open" width="650px">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="11">
            <el-form-item label="学号" prop="studentNo">
              <el-input v-model="form.studentNo" :disabled="true"/>
            </el-form-item>
          </el-col>  
          <el-col :span="11">
            <el-form-item label="姓名" prop="studentName">
              <el-input v-model="form.studentName" :disabled="true"/>
            </el-form-item>
          </el-col> 
          <el-col :span="11">
            <el-form-item label="宿舍栋号" prop="buildingNo">
              <el-input v-model="form.buildingNo" :disabled="true"/>
            </el-form-item>
          </el-col>  
          <el-col :span="11">
            <el-form-item label="宿舍号" prop="dormitoryNo">
              <el-input v-model="form.dormitoryNo" :disabled="true"/>
            </el-form-item>
          </el-col> 
          <el-col :span="11">
            <el-form-item label="押金" prop="deposit">
              <el-input v-model="form.deposit" placeholder="请输入押金" :disabled="isInput"/>
            </el-form-item>
          </el-col>  
          <el-col :span="11">
            <el-form-item label="缴费金额" prop="payAmount">
              <el-input v-model="form.payAmount" placeholder="请输入缴费金额"/>
            </el-form-item>
          </el-col> 
          <el-col >
            <el-form-item label="缴费日期" prop="payDate">
              <el-date-picker
                v-model="form.payDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>  
          <el-col>
            <el-form-item label="入住日期" prop="checkInDate">
              <el-date-picker
                v-model="form.checkInDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col> 
          <el-col>
            <el-form-item label="下次缴费日期" prop="nextPayDate">
              <el-date-picker
                v-model="form.nextPayDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>     
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看缴费历史 -->
    <el-dialog title="缴费历史" :visible.sync="payRecrodOpen" width="650px">
      <el-table v-loading="payRecrodLoading" :data="payRecrodList">
        <el-table-column label="学号" align="center" prop="studentNo" :show-overflow-tooltip="true" />
        <el-table-column label="学生姓名" align="center" prop="studentName" :show-overflow-tooltip="true" />
        <el-table-column label="缴费金额" align="center" prop="payAmount" :show-overflow-tooltip="true" />
        <el-table-column label="缴费日期" align="center" prop="payDate" :show-overflow-tooltip="true"/>
        <el-table-column label="入住日期" align="center" prop="checkInDate" :show-overflow-tooltip="true"/>
        <el-table-column label="下次缴费日期" align="center" prop="nextPayDate" :show-overflow-tooltip="true" />
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { findSdRentPageList,syncStudentData,renew,exportSdRent } from "@/api/smartdor/sdRent";
import { findPayRecordList} from "@/api/smartdor/sdPayRecord";


import { getToken } from "@/utils/auth";

export default {
  name: "sdRent",
  computed: {},
  data() {
    return {
        // 是否显示弹出层
        open: false,
        // 总条数
        total: 0,
        // 遮罩层
        loading: true,
        // 是否可输入
        isInput: false,
        submitDisabled:false,
        //表格数据
        sdRentList:[],
        // 楼栋号数据字典
        buildingNoOptions: [],
        storeyOptions:[
          // {
          //   dictLabel:"",
          //   dictValue:"1"
          // },
          {
            dictLabel:"二楼",
            dictValue:"2"
          },
          {
            dictLabel:"三楼",
            dictValue:"3"
          },
          {
            dictLabel:"四楼",
            dictValue:"4"
          },
          {
            dictLabel:"五楼",
            dictValue:"5"
          },
          {
            dictLabel:"六楼",
            dictValue:"6"
          },
        ],
        //缴费记录弹层
        payRecrodOpen:false,
        //缴费记录遮罩层
        payRecrodLoading:true,
        //缴费记录列表
        payRecrodList:[],
        // 缴费状态数据字典
        payStatusOption: [{
          label:"未缴费",
          value:"0"
        },{
          label:"已缴费",
          value:"1"
        },{
          label:"过期未缴费",
          value:"2"
        }
        ],
        // 查询参数
        queryParams: {
            pageNum: 1,
            pageSize: 10,
            studentNo: undefined,
            studentName: undefined,
            className: undefined,
            buildingNo: undefined,
            nextMonthExpire: undefined,
            storey:undefined,
            payStatus: undefined,
        },

        // 导入数据参数
        upload: {
          // 是否显示弹出层
          uploadOpen: false,
          // 弹出层标题
          uploadTitle: "上传学生住宿费缴费信息",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的学员信息数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/smartdor/rent/import/sdSentData",
        },
        // 表单参数
        form: {},
        // 表单校验
        rules:{
          payAmount:[
            { required: true, message: "缴费金额不能为空", trigger: "blur" }
          ],
          checkInDate:[
            { required: true, message: "入住日期不能为空", trigger: "blur" }
          ],
          nextPayDate:[
            { required: true, message: "下次缴费日期不能为空", trigger: "blur" }
          ]
        }
    };
  },

  created() {
    this.getList();
    this.getDicts("sys_building").then((response) => {
        this.buildingNoOptions = response.data;         
    });
  },
  methods: {
    /** 分页序号自增 */
    table_index(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    //获取住宿列表
    getList() {
      this.loading = true;
      findSdRentPageList(this.queryParams).then(response => {
        this.sdRentList = response.data.list;
        this.sdRentList.map((item) => {
          let payStatusArr =  { "0": "未缴费", "1": "已缴费", "2": "过期未缴费", null: "—","":"—"};
          item["payStatus"] = payStatusArr[item.payStatus];
          return item;
        })
        this.total = Number(response.data.total);
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 加载学生数据 */
    handleAdd(){
      this.loading = true;
      syncStudentData().then(response => {
          this.msgSuccess(response.data);
          this.getList();
          this.loading = false; 
      })
    },

    /** 缴费 */
    handleUpdate(row){
      this.open = true;
      this.reset();
      this.form.studentNo = row.studentNo;
      this.form.studentName = row.studentName;
      this.form.dormitoryNo = row.dormitoryNo;
      this.form.buildingNo = row.buildingNo;
      if(!(row.deposit == null)){
        this.form.deposit = row.deposit;
        this.isInput = true;
      }
    },
    // 缴费取消按钮
    cancel() {
      this.open = false;
      this.isInput = false;
      this.reset();
    },
    // 缴费表单重置
    reset() {
      this.form = {
        studentNo: undefined,
        studentName: undefined,
        buildingNo: undefined,
        dormitoryNo: undefined,
        deposit:undefined,
        payAmount: undefined,
        payDate: undefined,
        checkInDate:undefined,
        nextPayDate: undefined,
      };
      this.resetForm("form");
    },
    //缴费提交
    submitForm:function(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          renew(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(response.data);
              this.open = false;
              this.isInput = false;
              this.getList();
            } else {
              this.msgError(response.data);
            }   
          })
        }
      });
    },
    /** 查看缴费历史 */
    handleViewPayRecorList(row){
      this.payRecrodOpen = true;
      this.payRecrodLoading = true;
      findPayRecordList(row.studentNo).then(resp => {
        this.payRecrodList = resp.data;
        this.payRecrodLoading = false;
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('确认是否导出?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSdRent(queryParams);
        }).then(response => {
          this.fileDownload(response.data, "宿舍缴费记录.xlsx");
        }).catch(function() {
        });
    },

    /**
     * 文件上传
     */
    // 导入按钮操作
    handleImport() {
        this.upload.uploadTitle = "导入住宿费缴费信息";
        this.upload.uploadOpen = true;
        this.submitDisabled = false;
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
        this.submitDisabled = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
        this.upload.uploadOpen = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        if(response.data == null || response.data == undefined || response.data.length == 0){
          this.$alert("所有学生的缴费信息均已导入", "导入结果", {
            dangerouslyUseHTMLString: true,
          })
        }else{
          // let msg = "学生【"
          // response.data.map((item,index) => {
          //   msg.concat(item).concat(",")
          // })
          // msg.concat("】未导入")
          this.$alert("部分学生缴费信息导入成功", "导入结果", {
            dangerouslyUseHTMLString: true,
          })
        }

        this.getList();
    },
    // 提交上传文件
    submitFileForm() {
        this.$refs.upload.submit();
    },
  },
};
</script>
    
<style>
</style>